// pages/itemfile/itemfile.js
const utils = require('../../utils/util.js');
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
      textArr:[],
      aduioArr:[],
      videoArr:[],
      imgArr:[],
      currentShow:0,
      playType:'',
      audiostate:'stop',
      currentAudioname:'',
      currentAudioduration:'',
      currentAudiocurrentTime:'',
      currentAudiobuffered:0,
      slidermax:0,
      slidervalue:0,
      sliderstate:false,
      currentvideourl:'',
      videobg:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      wx.showLoading({
          title: '加载中',
          icon: 'loading'
      })
      utils.ajax(app.globalData.ROOT_URL + 'dsfiles/'+options.id, {
          header: {
              Accept: "application/vnd.dct.v1+json"
          }
      }, (res) => {
          wx.hideLoading();
          if (res.state == "1") {
              this.setData({
                  textArr: res.data.pdfs,
                  aduioArr: this.resetTime(res.data.audios),
                  videoArr: this.resetTime(res.data.videos),
                  imgArr: res.data.images,
              })
          }else{
              wx.showToast({
                  title: '获取数据失败，请重试',
                  icon:'none'
              })
          }
      },err=>{
          wx.hideLoading();
          wx.showToast({
              title: '获取数据失败，网络错误',
              icon: 'none'
          })
      })
  },
  //播放音频组件
  //点击音频播放
  openAudio:function(e){
      let src = e.currentTarget.dataset.audiourl;
      if (this.audioctrl.src == src){
          this.setData({
              currurl: src,
              audiostate: 'play',
              playType:'audio',
              slidermax: parseInt(e.currentTarget.dataset.duration),
              slidervalue: 0
          })
          this.audioctrl.play();
      }else{
          this.audioctrl.src = src;
          this.audioctrl.play();
          this.setData({
              currentAudioname: e.currentTarget.dataset.name,
              currentAudioduration: utils.getWaiting(parseInt(e.currentTarget.dataset.duration), true),
              currentAudiocurrentTime: utils.getWaiting(0, true),
              currentAudiobuffered: this.audioctrl.buffered / 1000 /this.audioctrl.duration * 100,
              playType: 'audio',
              currurl: src,
              audiostate: 'play',
              slidermax:parseInt(e.currentTarget.dataset.duration),
              slidervalue:0
          })
      }
  },
  //音频暂停
  puaseAudio:function(){
    this.audioctrl.pause();
    this.setData({
        currurl:'pause',
        audiostate:'pause'
    })
  },
  //音频重启
  audioReset:function(){
      this.audioctrl.play();
      this.setData({
          currurl: this.audioctrl.src,
          audiostate: 'play',
      })
  },
  //音频停止
  audioStop:function(){
      this.audioctrl.stop();
      this.setData({
          playType: '',
          audiostate: 'stop',
          currurl: 'stop',
      })
  },
  //滚动条触摸开始
  sliderStart:function(){
      this.setData({
          sliderstate:true
      })
  },
  //音频进度条拖动结束
  sliderEnd: function (e) {
      this.setData({
          sliderstate:false
      })
      this.seek()
  },
  sliderChange: function (e) {
      this.seek()
  },
  ///音频进度条拖动取消 
  sliderCancel: function (e) {
      this.seek()
  },
  //音频进度条点击
  sliderTap:function(e){
      this.seek(e.detail.value);
  },
  //调整播放进度
  seek: function (value) {
      if(value>0){
          let value = this.wxzxSlider.properties.value;
      }else{
          let value = this.wxzxSlider.properties.value;
      }
      let seek_time = Math.ceil(value);
      let pass_time = utils.getWaiting(seek_time,true);
      this.audioctrl.seek(seek_time);
      setTimeout(res=>{
          this.setData({
              slidervalue: seek_time,
              currentAudiocurrentTime: pass_time,
          })
      },250)
  },
  //视频播放
  openVideo:function(e){
     let data = e.currentTarget.dataset;
     this.videoctrl = wx.createVideoContext("video1");
     this.audioctrl.stop();
     this.setData({
         playType:'video',
         currentvideourl:data.videourl,
         videobg: data.bgurl,
         audiostate: 'stop',
         currurl: data.videourl,
     })
     setTimeout(()=>{
         this.videoctrl.play(); 
     },500)  
  },
  closeVideo:function(e){
      this.videoctrl.pause();
      this.videoctrl.exitFullScreen();
      this.videoctrl.seek(0);
      this.setData({
          playType:'',
          currurl: '',
      })
  },
  pauseVideo:function(){
      this.videoctrl.pause();
      this.setData({
          currurl: ''
      })
  },
  //视频播放
  videoPlay:function(e){
      let url = this.data.currentvideourl;
      this.setData({
          currurl: url,
      })
  },
  //视频暂停
  videoPause:function(){
      this.setData({
          currurl:"",
      })
  },
  //视频结束
  videoEnd:function(){
      this.videoctrl.exitFullScreen();
      this.setData({
          playType: '',
          currurl: '',
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function () {
        let that=this;
        this.audioctrl = wx.createInnerAudioContext();
        this.wxzxSlider = this.selectComponent("#wxzxSlider");
        //音频进度更新
        this.audioctrl.onPlay(function (e) {
           that.audioctrl.onTimeUpdate(function(res){
               if (that.data.sliderstate == true){
                   return;
               }else{
                   let s = that.audioctrl.buffered / 1000 / that.audioctrl.duration * 100;
                   that.setData({
                       currentAudiocurrentTime: utils.getWaiting(Math.ceil(that.audioctrl.currentTime), true),
                       currentAudiobuffered: s,
                       slidervalue: Math.ceil(that.audioctrl.currentTime),
                   })
               }
            })
        })
        this.audioctrl.onPause(function(){
            that.setData({
                currurl: 'pause',
                audiostate: 'pause',
                slidervalue: Math.ceil(that.audioctrl.currentTime)
            })
        })
        this.audioctrl.onEnded(function(e){
            that.setData({
                playType: '',
                audiostate: 'stop',
                currurl: 'stop',
                slidervalue:0,
                slidermax:0,
                currentAudiobuffered:0
            })
        })
    },
    resetTime: function (data) {
        for (let i = 0, l = data.length; i < l;i++) {
            if (!data[i].duration) {
                return false;
            } else {
                let n = utils.getWaiting(data[i].duration,true);
                data[i].showTime = n;
            }
        }
        return data;
    },
    openDoc:function(e){
        let url =e.currentTarget.dataset.docurl;
        wx.showLoading({
            title: '加载中',
        })
        wx.downloadFile({
            url: url,
            success: function (res) {
                var filePath = res.tempFilePath
                wx.openDocument({
                    filePath: filePath,
                    fileType:"pdf",
                    success: function (res) {
                        wx.hideLoading();
                    },
                    fail:function(e){
                        wx.hideLoading();
                        console.log(e)
                        wx.showToast({
                            title: '打开失败，文件信息错误',
                            icon:'none'
                        })
                    }
                })
            },
            fail:function(e){
                wx.hideLoading();
                wx.showToast({
                    title: '加载失败，网络错误',
                    icon: 'none'
                })
            }
        })
    },
    toggleDot:function(e){
        this.setData({
            currentShow:e.detail.current
        })
    },
    toggleShow:function(e){
        if (e.target.dataset.index == this.currentShow){
            return false;
        }
        this.setData({
            currentShow:e.target.dataset.index
        })
    },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let app = getApp();
    app.loginState();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
      this.audioctrl.destroy();
      this.setData({
          playType:'',
          currurl:''
      })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  }
})